<?php


namespace XinYaTable\Forms\Controllers;


use XinYaTable\Forms\Services\FormCustomViewService;
use Illuminate\Http\Request;

class FormCustomViewController extends BaseController
{
    public $exceptAllAction = true;
    
    private $_formCustomViewService;

    public function __construct(FormCustomViewService $formCustomViewService)
    {
        $this->_formCustomViewService = $formCustomViewService;
    }

    /**
     * @apiDefine customView 自定义显示列管理
     */
    /**
     * @apiVersion v1.0.0
     * @api        {get} /api/forms/{id}/view 获取用户表单自定义显示列 GET
     * @apiName    getUserCustomView
     * @apiGroup   customView
     * @apiParam {Number} id 表单id，参数在url上传值
     * @apiSuccessExample {JSON} 结果描述
     * {
     *      "code": 0,
     *      "msg": "请求成功。",
     *      "data": [
     *          {
     *              "id": 4,
     *              "field_id": 1,
     *              "field_name": "name",//字段名称
     *              "field_type": "text",//字段类型
     *              "title": "城市",//标题
     *              "width": 120,//宽度 单位px
     *              "order": 1，
     *              "sortable": 1,//是否可排序
     *              "resizable": 1,//是否可调整大小
     *              "editable": 1,//是否可编辑
     *              "display_format": "yyyy-mm-dd"//显示格式
     *          },{...}
     *      ]
     * }
     */
    /**
     * @param Request $request
     * @param int     $formId
     *
     * @return mixed
     */
    public function getUserCustomView(Request $request, $formId)
    {
        return $this->_formCustomViewService->userCustomViewByFormId($formId, $request->user()->id);
    }

    /**
     * @apiVersion v1.0.0
     * @api        {put} /api/forms/{id}/view 修改用户表单自定义显示列 PUT
     * @apiName    updateUserCustomView
     * @apiGroup   customView
     * @apiParam {Number} id 表单id，参数在url上传值
     * @apiParam {Array} list
     * @apiSuccessExample{JSON} 参数描述
     *{
     *      "list":[//调整后的表头数据
     *          {
     *              "field_id": 1,
     *              "field_name": "name",
     *              "field_type":"number",
     *              "title": "城市",
     *              "width": 120,
     *              "order": 1，
     *              "sortable": 1,//是否可排序
     *              "resizable": 1,//是否可调整大小
     *              "editable": 1,//是否可编辑
     *              "display_format": "yyyy-mm-dd"//显示格式
     *          },{...}
     *      ]
     * }
     * @apiSuccessExample {JSON} 结果描述
     * {
     *      "code": 0,
     *      "msg": "请求成功。",
     *      "data": [
     *          {
     *              "id": 4,
     *              "field_id": 1,
     *              "field_name": "name",//字段名称
     *              "field_type": "text",//字段类型
     *              "title": "城市",//标题
     *              "width": 120,//宽度 单位px
     *              "order": 1，
     *              "sortable": 1,//是否可排序
     *              "resizable": 1,//是否可调整大小
     *              "editable": 1,//是否可编辑
     *              "display_format": "yyyy-mm-dd"//显示格式
     *          },{...}
     *      ]
     * }
     */
    /**
     * @param Request $request
     * @param int     $formId
     * @param string  $tableName
     *
     * @return mixed
     * @throws \XinYaTable\Forms\Exceptions\FormException
     */
    public function updateUserCustomView(Request $request, $tableName)
    {
        return $this->_formCustomViewService->updateUserCustomView(
            $request->get('list') ?? [],
            $tableName,
            $request->user()->id
        );
    }

    /**
     * @param Request $request
     * @param int     $id
     *
     * @return mixed
     * @throws \XinYaTable\Forms\Exceptions\FormException
     */
    public function resizeCustomView(Request $request, int $id)
    {
        return $this->_formCustomViewService->resizeCustomView(
            $id,
            $request->get('width'),
            $request->user()->id
        );
    }

}